package com.example.apiproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.apiproject.dao.ArticlesDao;
import com.example.apiproject.dao.UserDao;
import com.example.apiproject.entity.Activities;
import com.example.apiproject.entity.Articles;
import com.example.apiproject.entity.form.AddPostForm;
import com.example.apiproject.entity.form.PostListForm;
import com.example.apiproject.service.ArticlesService;
import com.example.apiproject.utils.R;
import com.example.apiproject.utils.AppPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * (Articles)表控制层
 *
 * @author makejava
 * @since 2023-09-23 13:40:56
 */
@RestController
@RequestMapping("/articles")
public class ArticlesController {
    /**
     * 服务对象
     */
    @Resource
    private ArticlesService articlesService;
    @Autowired
    private ArticlesDao articlesDao;


    /**
     * 判断成功的寻搭开始时间是否已过24小时，是的话返回true，前端做提醒
     * @param articleId
     * @return
     */
//    @GetMapping("/timeRemind")
//    public R remind(Integer articleId){
//        LocalDateTime time = LocalDateTime.now().plusHours(-24);
//        return R.ok().put("result",articlesDao.queryTobeReminded(articleId,time));
//    }
    @GetMapping("/queryByType")
    public R queryByType(int type,@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        String str="";
        if(type==1){str="学习";}
        if(type==2){str="兴趣爱好";}
        if(type==3){str="旅行";}
        if(type==4){str="休闲娱乐";}
        return R.ok().put("result",articlesService.queryPageListBytype(pageNum,pageSize,str));}
    /**
     * 根据用户id查询最新的一条寻搭帖子
     * @param userid
     * @return
     */
    @GetMapping("/queryLast")
    public R queryLastArticleByUserid(@RequestParam int userid){
        return R.ok().put("result",articlesDao.queryLastByUserId(userid));
    }

    /**
     * 根查询当前用户Id的寻搭文章
     * @param request 用户id
     * @return 文章集合
     */
    @GetMapping("/user")
    public R queryByUserId(HttpServletRequest request,  @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){

        return R.ok().put("result",articlesService.queryByUserId(request,pageNum,pageSize));
    }

    @GetMapping("/recent")
    public R queryRecent(HttpServletRequest request){
        return R.ok().put("result",articlesService.queryRecent(request));
    }

    @GetMapping("/queryall")
    public R list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                  @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize
    ){

        AppPageUtils page = articlesService.queryPageList(pageNum,pageSize);


        return R.ok().put("result", page);
    }
    /**
     * 通过label模糊查询
     *
     * @param label
     * @return 实例对象
     */
    @GetMapping("/search")
    public R searchByLabel(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,@RequestParam("label") String label) {
//        List<Articles> articlesList = articlesService.queryByLabel(label);
        AppPageUtils page = articlesService.queryByLabel(pageNum,pageSize,label);
        return R.ok().put("result",page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById")
    public R queryById( String id) {
        id = id.replace("\"","");
        System.out.println(id);
        return R.ok().put("res",this.articlesService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param articles 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public R add(@RequestBody AddPostForm articles, HttpServletRequest request) {

        int id = articlesService.insert(articles,request);
        if(id==0){
            return R.error("发帖失败");
        }
        return R.ok().put("result",id);
    }
//    测试添加帖子标签
//    @PostMapping("/addArticle")
//    public String addArticle(@RequestParam("title") String title, @RequestParam("labels") String labels) {
//        Articles article = new Articles();
//        UUID uuid = UUID.randomUUID();
//        article.setId(uuid.toString());
//        article.setUserId(123);
//        article.setTitle(title);
//        article.setLabels(labels);
//        articlesService.addArticle(article);
//
//        return "redirect:/success";
//    }

//    /**
//     * 编辑数据
//     *
//     * @param articles 实体
//     * @return 编辑结果
//     */
//    @PutMapping
//    public ResponseEntity<Articles> edit(Articles articles) {
//        return ResponseEntity.ok(this.articlesService.update(articles));
//    }

    @PostMapping("/update")
    public R update(@RequestBody Articles articles){
        return R.ok().put("result",articlesService.update(articles));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */

    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.articlesService.deleteById(id));
    }



}

